import { useEffect, useState } from 'react'
import { getProductDevelopmentSeason } from '@/api/goods'
export interface IProDevSeason {
  seasonId: number
  seasonName: string
  year: string
}

const useGetProDevSeason = (key: 'seasonId' | 'seasonName' = 'seasonId') => {
  const [data, setData] = useState<(IOptionItem & { seasonId: number })[]>([])
  useEffect(() => {
    getData()
  }, [])
  async function getData() {
    const res = await getProductDevelopmentSeason<IProDevSeason>()
    if (res.code === 200) {
      const data =
        res?.data
          ?.map((item) => {
            return {
              label: item.seasonName,
              value: item[key],
              year: item.year,
              seasonId: item.seasonId,
            }
          })
          ?.sort((a: any, b: any) => b?.seasonId - a?.seasonId) || []
      setData(data)
      return data
    }
  }
  return data
}

export default useGetProDevSeason
